﻿using log4net;
using System.Diagnostics;
using System.Reflection;

namespace Armon.Lib.Logger
{
    public class Logger : ILogger
    {
        public readonly ILog log;
        public Logger()
        {
            this.log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public void Info(string log)
        {
            log = AppendClassLine(log);
            this.log.Info(log);
        }

        public void Error(string log)
        {
            log = AppendClassLine(log);
            this.log.Error(log);
        }

        public void Error(Exception e)
        {
            var msg = $"{e.Message}\r\n{e.StackTrace}";
            this.Error(msg);
        }

        public void Debug(string log)
        {
            log = AppendClassLine(log);
            this.log.Debug(log);
        }

        public void Fatal(string log)
        {
            log = AppendClassLine(log);
            this.log.Fatal(log);
        }

        public void Warn(string log)
        {
            log = AppendClassLine(log);
            this.log.Warn(log);
        }

        private string AppendClassLine(string log)
        {
            var st = new StackTrace(true);
            var sf = st.GetFrame(2);
            return $"{log} [{Path.GetFileName(sf.GetFileName())}:{sf.GetFileLineNumber()}]";
        }
    }
}
